/*
 * Copyright (C) 2009 Jiannan Lu
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package restsql;

import com.restsql.RestSQLHttpServerHandler;
import com.restsql.db.Database;
import com.restsql.xmlconfig.XMLConfigurator;
import com.sun.net.httpserver.HttpServer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jiann.lu@yahoo.com
 */
public class Main {

    /** The listening address if specified. */
    private static final String URL_ADDRESS = "/";
    /** The listening port if specified. */
    private static int PORT = 8384;
    private static String DB_CLASS_NAME = "";
    private static String DB_CONN_STR = "";
    private static String DB_USER = "";
    private static String DB_PWD = "";
    public static String HOST_URL = "http://127.0.0.1:8384";
    private static RestSQLHttpServerHandler restSQLHttpServerHandler;
    private static HttpServer server;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        
        XMLConfigurator xmlConfigurator = new XMLConfigurator();
        try {
            xmlConfigurator.parse("Appconfig.xml");

            Properties properties = xmlConfigurator.getProps();
            PORT = Integer.parseInt(properties.getProperty("PORT"));
            DB_CLASS_NAME = properties.getProperty("DB_CLASS_NAME");
            DB_CONN_STR = properties.getProperty("DB_CONN_STR");
            DB_USER = properties.getProperty("DB_USER");
            DB_PWD = properties.getProperty("DB_PWD");
            server = HttpServer.create(new InetSocketAddress(PORT), 0);

            restSQLHttpServerHandler = new RestSQLHttpServerHandler(new Database(DB_CLASS_NAME, DB_CONN_STR,DB_USER,DB_PWD));
            server.createContext(URL_ADDRESS,restSQLHttpServerHandler );
            server.setExecutor(null); // allow default executor to be created
            server.start();

           BufferedReader stdIn=new BufferedReader(new InputStreamReader(System.in));
           String strInput=null;
           strInput=stdIn.readLine();
           if(strInput.toUpperCase().equals("QUIT")){
               System.exit(1);
           }
           
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}
